<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-28
 * Time: 下午9:57
 * To change this template use File | Settings | File Templates.
 */
class iverycd_com extends MY_Model
{
    private $data = array();

    /**
     * 分析数据
     *
     * @param array $arr
     * @return bool
     */
    public function analysis(array $arr)
    {
        $content = $this->getFileContent($arr['file_name']);
        //$download = array();

        if (empty ($content)) return false;

        //来源网站ID
        $this->data['source_id'] = $arr['source_id'];

        //名称
        preg_match("/<div class=\"page_tit\">.*<h3>(.*)<\/h3>/sU", $content, $name);
        $this->data['china_name'] = isset ($name[1]) ? $name[1] : '';
        unset ($name);

        //分类
        preg_match("/当前位置：.*<\/span><a href=\".*\">(.*)<\/a>/sU", $content, $class);
        $this->data['class'] = isset ($class[1]) ? $this->getClass($class[1]) : '';
        unset ($class);

        //导演
        preg_match("/【导演】(.*)【/sU", $content, $director);
        if (!isset ($director[1])) {
            $director = array();
            preg_match("/◎导.*演(.*)◎/sU", $content, $director);
        }
        if (!isset ($director[1])) {
            $director = array();
            preg_match("/导.*演:(.*)<br.*>/sU", $content, $director);
        }
        if (!isset ($director[1])) {
            $director = array();
            preg_match("/导演[:|：](.*)[演员|动作指导|编剧]/sU", $content, $director);
        }
        $this->data['director'] = isset ($director[1]) ? $director[1] : '';
        unset ($director);

        //演员
        preg_match("/【主演】(.*)【/sU", $content, $starring);
        if (!isset ($starring[1])) {
            $starring = array();
            preg_match("/◎主.*演(.*)◎/sU", $content, $starring);
        }
        if (!isset ($starring[1])) {
            $starring = array();
            preg_match("/[演员|主演][:|：](.*)[地区|主要演员|导演]/sU", $content, $starring);
        }
        $this->data['starring'] = isset ($starring[1]) ? $starring[1] : '';
        unset ($starring);

        //地区
        preg_match("/【国家】(.*)【/sU", $content, $area);
        if (!isset ($area[1])) {
            $area = array();
            preg_match("/◎国.*家(.*)<br.*>/sU", $content, $area);
        }
        if (!isset ($area[1])) {
            $area = array();
            preg_match("/地区:(.*)语言:/sU", $content, $area);
        }
        $this->data['area'] = isset ($area[1]) ? $area[1] : '';
        unset ($area);

        //类型
        preg_match("/<p>所属分类：(.*)<\/p>/sU", $content, $types);
        preg_match_all("/<a.*>(.*)<\/a>/sU", $types[1], $type);
        $this->data['type'] = implode('|', array_filter($type[1]));
        unset ($type, $types);

        //语言
        preg_match("/【语言】(.*)【/sU", $content, $language);
        if (!isset ($language[1])) {
            $language = array();
            preg_match("/语言:(.*)发布组/sU", $content, $language);
        }
        $this->data['language'] = isset ($language[1]) ? $language[1] : '';
        unset ($language);

        //发布时间
        preg_match("/<p>发布时间：(.*)<\/p>/sU", $content, $publicTime);
        if (!isset ($publicTime[1])) {
            $publicTime = array();
            preg_match("/◎年.*代(.*)◎/sU", $content, $publicTime);
        }
        if (!isset ($publicTime[1])) {
            $publicTime = array();
            preg_match("/发行日期:(.*)导演/sU", $content, $publicTime);
        }
        $this->data['public_time'] = isset ($publicTime[1]) ? $publicTime[1] : '';
        unset ($publicTime);

        //简介
        preg_match("/<div class=\"main_content\">.*<\/table>  (.*)<div id=\"wumiiLikeRecBtnDiv\">/sU", $content, $drama);
        $this->data['drama'] = isset ($drama[1]) ? $drama[1] : '';

        //海报
        preg_match("/<img.*src=\"(.*)\".*>/sU", $drama[1], $posters);
        //$this->data['img_addr'] = (strpos($posters[1], 'http://') === false) ? 'http://www.iverycd.com'.$posters[1] : $posters[1];
        //*
        if (isset ($posters[1])) {
            $this->data['img_addr'] = (strpos($posters[1], 'http://') === false) ? 'http://www.iverycd.com'.$posters[1] : $posters[1];
        } else {
            $this->data['img_addr'] = '';
        }//*/
        unset ($posters);
        unset ($drama);

        //下载链接
        preg_match_all("/<tr class=\"ed2kitem\">.*<td class=\"ed2kurl\">.*<a.*href=\"(.*)\".*>(.*)<\/a>.*<td class=\"ed2ksize\".*>(.*)<\/td>/sU", $content, $downloads);

        foreach ($downloads[1] as $dlk=>$dlv) {
            $download[] = array(
                'name' => $downloads[2][$dlk],
                'link' => $downloads[1][$dlk],
                'size' => $downloads[3][$dlk],
            );
        }
        $this->data['download'] = $download;
        unset ($downloads);
        $download = array();unset ($download);
        $content = '';unset ($content);

        //echo '<pre>';print_r($area);//exit;
        //echo '<pre>';print_r($this->data);echo $content;exit;
    }

    /**
     * 保存数据
     *
     * @return mixed
     */
    public function save()
    {
        //echo '<pre>';print_r($this->data);exit;
        $data = array(
            'china_name' => $this->data['china_name'],
            'english_name' => '',
            'class' => $this->data['class'],
            'director' => $this->data['director'],
            'starring' => $this->data['starring'],
            'area' => $this->data['area'],
            'type' => $this->data['type'],
            'update_status' => 1,
            'public_time' => $this->data['public_time'],
            'version' => 'DVD',
            'language' => $this->data['language'],
            'poster_addr' => '',
            'drama' => addslashes($this->data['drama']),
            'episode' => '1',
            'dub' => '',
            'status' => 1,
            'uniqid' => md5('iverycd_com'.$this->data['source_id']),
            'img_addr' => $this->data['img_addr'],
            'source' => 'iverycd_com',
            'source_id' => $this->data['source_id'],
            'score' => '',
            'subtitle' => '',
            'release' => '',
            'update_time' => date('Y-m-d H:i:s'),
            'create_time' => date('Y-m-d H:i:s'),
        );

        $this->db->insert('movie', $data, true);
        $mId = $this->db->insert_id();
        if ($mId) {
            $this->categoryCorrespond($mId, $this->data['class'], $this->data['type']);
        }
        $this->saveDownLoad($mId);
        unset ($data);

        return $mId;
    }

    /**
     * 保存下载链接地址
     *
     * @param $mId
     */
    public function saveDownLoad($mId)
    {
        if (empty ($this->data['download']) || !is_array($this->data['download'])) {
            $this->data['download'] = array();
        }

        if (empty ($mId)) {
            $this->load->model('movie');

            $data = $this->movie->getMovieByChinaName($this->data['china_name']);
            if (!empty ($data)) {
                $mId = $data['id'];
            }
        }

        foreach ($this->data['download'] as $v) {
            if (empty ($v) || !is_array($v)) continue;

            //parse_str($v['downaddr'], $tmpArr);
            //echo '<pre>';print_r($tmpArr);//exit;

			//$str = '';
			//isset ($tmpArr['cid']) && $str .= 'cid='.$tmpArr['cid'];
			//isset ($tmpArr['xzurl']) && $str .= '&xzurl='.$tmpArr['xzurl'];
			//$str .= '&mc='.$v['name'];

			//echo $str."\n";exit;
            //$downloadLink = getDownLoadUrl($str);
            $data = array(
                'video_id' => $mId,
                'name' => $v['name'],
                'download_addr' => $v['link'],//$downloadLink,//
                'size' => $v['size'],
                'uniqueid' => md5($mId.$v['name'].$v['link'].$v['size']),
                'source_download_link' => $v['link'],
                'create_time' => date('Y-m-d H:i:s'),
            );
            //echo '<pre>';print_r($data);exit;
            $this->db->insert('download_list', $data, true);
            $downloadId = $this->db->insert_id();
            if ($downloadId) {
                $this->updateResource($mId);
            }
        }
        $this->data = array();
        $data = array();unset ($data);
    }
}
